<template>
    <div class="city-list" ref="wrapper">
        <div>
            <div class="area">
                <div class="title border-topbottom">当前城市</div>
                <div class="button-list">
                    <div class="button-wrapper">
                        <div class="button">{{ this.cityList }}</div>
                    </div>
                </div>
            </div>

            <div class="area">
                <div class="title border-topbottom">热门城市</div>
                <div class="button-list">
                    <div
                        class="button-wrapper"
                        v-for="hotCity in hotCities"
                        :key="hotCity.id"
                        @click="updateCity(hotCity.name)"
                    >
                        <div class="button">{{ hotCity.name }}</div>
                    </div>
                </div>
            </div>

            <div class="area" v-for="(item, key) in cities" :key="key" :ref="key">
                <div class="title border-topbottom">{{ key }}</div>
                <div class="item-list">
                    <div class="item border-bottom" v-for="city in item" :key="city.id" @click="updateCity(city.name)">{{ city.name }}</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import Bscroll from 'better-scroll'
export default {
    name: 'CityList',
    computed: {
        ...mapState({
            cityList: 'city'
        })
    },
    props: {
        hotCities: Array,
        cities: Object,
        letter: String
    },
    methods: {
        updateCity(val) {
            // this.$store.commit('changeCity', val)
            this.changeCity(val)
            this.$router.push({
                path: '/'
            })
        },
        ...mapMutations(['changeCity'])

    },
    mounted() {
        this.scroll = new Bscroll(this.$refs.wrapper)
    },
    watch: {
        letter() {
            if (this.letter) {
                const el = this.$refs[this.letter][0]
                this.scroll.scrollToElement(el)
            }
        }
    }
}
</script>

<style lang="scss" scoped>
@import '~styles/varibles.scss';
.border-topbottom{
    &:before{
        background: #ccc;
    }
    &:after{
        background: #ccc;
    }
}
.border-bottom{
    &:before{
        background: #ccc;
    }
}
.city-list{
    overflow: hidden;
    position: absolute;
    top: 1.58rem;
    left: 0;
    right: 0;
    bottom: 0;
    .title{
        line-height: 0.64rem;
        background: #eee;
        padding-left: 0.2rem;
        color: rgb(0, 0, 0);
        font-size: 0.08rem;
    }
    .button-list{
        padding: .1rem .6rem .1rem .1rem;
        overflow: hidden;
        .button-wrapper{
            float: left;
            width: 33.33%;
            .button{
                text-align: center;
                margin: 0.1rem;
                border: 0.02rem solid #ccc;
                padding: 0.1rem 0;
                border-radius: 0.06rem;
            }
        }
    }
    .item-list{
        .item{
            line-height: 0.76rem;
            padding-left: 0.2rem;
        }
    }
}

</style>
